import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import "qrc:/qml/components" as Components
import "qrc:/qml/styles" as Styles

/**
 * @brief 仿真日志面板
 * @details 用于SimulatorScreen侧边栏，展示仿真运行日志
 */
Rectangle {
    id: root
    color: Styles.Theme.surface

    // 外部接口
    property var logList: []

    signal clearLogsRequested()

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: Styles.Theme.spacingNormal
        spacing: Styles.Theme.spacingSmall

        // ==================== 标题栏 ====================
        RowLayout {
            Layout.fillWidth: true
            spacing: Styles.Theme.spacingSmall

            Text {
                text: qsTr("仿真日志")
                color: Styles.Theme.textPrimary
                font.pixelSize: Styles.Theme.fontSizeLarge
                font.weight: Font.Bold
                Layout.fillWidth: true
            }

            // 日志计数
            Rectangle {
                Layout.preferredWidth: 50
                Layout.preferredHeight: 24
                radius: 12
                color: Styles.Theme.backgroundAlternate
                border.color: Styles.Theme.border
                border.width: 1

                Text {
                    anchors.centerIn: parent
                    text: logList.length.toString()
                    color: Styles.Theme.textPrimary
                    font.pixelSize: 11
                    font.weight: Font.Bold
                }
            }

            // 清空按钮
            Components.ActionButton {
                text: qsTr("清空")
                buttonType: "danger"
                width: 60
                height: 28
                onClicked: clearLogsRequested()
            }
        }

        // ==================== 日志级别筛选 ====================
        RowLayout {
            Layout.fillWidth: true
            spacing: Styles.Theme.spacingSmall

            Repeater {
                model: [
                    { level: "all", text: qsTr("全部"), color: Styles.Theme.textSecondary },
                    { level: "info", text: qsTr("信息"), color: Styles.Theme.info },
                    { level: "warn", text: qsTr("警告"), color: Styles.Theme.warning },
                    { level: "error", text: qsTr("错误"), color: Styles.Theme.danger }
                ]

                delegate: Rectangle {
                    Layout.fillWidth: true
                    height: 28
                    radius: 4
                    color: logLevelFilter === modelData.level ? modelData.color : "transparent"
                    border.color: modelData.color
                    border.width: 1

                    Text {
                        anchors.centerIn: parent
                        text: modelData.text
                        color: logLevelFilter === modelData.level ? Styles.Theme.textOnPrimary : modelData.color
                        font.pixelSize: 10
                        font.weight: Font.Bold
                    }

                    MouseArea {
                        anchors.fill: parent
                        cursorShape: Qt.PointingHandCursor
                        onClicked: logLevelFilter = modelData.level
                    }
                }
            }
        }

        // ==================== 日志列表 ====================
        ScrollView {
            Layout.fillWidth: true
            Layout.fillHeight: true
            clip: true

            ListView {
                id: logListView
                model: getFilteredLogs()
                spacing: 2

                // 自动滚动到底部
                onCountChanged: {
                    if (autoScrollEnabled) {
                        Qt.callLater(() => positionViewAtEnd())
                    }
                }

                delegate: Rectangle {
                    width: logListView.width
                    height: logContent.implicitHeight + 16
                    color: index % 2 === 0 ? Styles.Theme.background : Styles.Theme.backgroundSecondary
                    radius: 2

                    RowLayout {
                        anchors.fill: parent
                        anchors.margins: 8
                        spacing: 8

                        // 时间戳
                        Text {
                            text: modelData.time
                            color: Styles.Theme.textTertiary
                            font.pixelSize: 9
                            font.family: "Consolas"
                            Layout.preferredWidth: 60
                        }

                        // 级别标签
                        Rectangle {
                            Layout.preferredWidth: 40
                            Layout.preferredHeight: 16
                            radius: 8
                            color: getLogLevelColor(modelData.level)

                            Text {
                                anchors.centerIn: parent
                                text: modelData.level.toUpperCase()
                                color: Styles.Theme.textOnPrimary
                                font.pixelSize: 8
                                font.weight: Font.Bold
                            }
                        }

                        // 日志内容
                        Text {
                            id: logContent
                            text: modelData.message
                            color: Styles.Theme.textPrimary
                            font.pixelSize: 11
                            font.family: "Consolas"
                            wrapMode: Text.Wrap
                            Layout.fillWidth: true
                        }
                    }
                }

                // 空状态提示
                Label {
                    visible: logListView.count === 0
                    anchors.centerIn: parent
                    text: qsTr("暂无日志")
                    color: Styles.Theme.textSecondary
                    font.pixelSize: Styles.Theme.fontSizeNormal
                }
            }
        }

        // ==================== 底部控制栏 ====================
        RowLayout {
            Layout.fillWidth: true
            spacing: Styles.Theme.spacingSmall

            // 自动滚动开关
            CheckBox {
                id: autoScrollCheckBox
                checked: autoScrollEnabled
                text: qsTr("自动滚动")
                font.pixelSize: 10
                onCheckedChanged: autoScrollEnabled = checked
            }

            Item {
                Layout.fillWidth: true
            }

            // 导出日志按钮
            Components.ActionButton {
                text: qsTr("导出")
                buttonType: "secondary"
                width: 60
                height: 28
                enabled: logList.length > 0
                onClicked: exportLogs()
                ToolTip.visible: hovered
                ToolTip.text: qsTr("导出日志到文件")
            }
        }
    }

    // ==================== 内部状态 ====================

    property string logLevelFilter: "all"
    property bool autoScrollEnabled: true

    // ==================== 辅助函数 ====================

    function getFilteredLogs() {
        if (logLevelFilter === "all") {
            return logList
        }
        return logList.filter(log => log.level === logLevelFilter)
    }

    function getLogLevelColor(level) {
        const colors = {
            "error": Styles.Theme.danger,
            "warn": Styles.Theme.warning,
            "info": Styles.Theme.info,
            "debug": Styles.Theme.textSecondary
        }
        return colors[level] || Styles.Theme.textSecondary
    }

    function exportLogs() {
        // TODO: 实现日志导出功能
        console.log("Export logs requested")
    }
}
